package com.oamicnet.servicetwo.server.manager;

import com.oamicnet.serviceone.api.account.AccountInfoApi;
import com.oamicnet.servicetwo.server.service.ProductInfoService;
import org.apache.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ProductInfoManager{
    @Autowired
    private AccountInfoApi accountInfoApi;

    @Autowired
    private ProductInfoService productInfoService;

    /**
     * 分布式事务控制(AT模式)
     *
     * @param id
     * @param stock
     * @return
     */
    @GlobalTransactional
    public int updateProductStock(Long id, Integer stock) {

        //更新账户余额
        accountInfoApi.updateBalance("CZ0001", -stock * 2000d);//库存量*单价

        int i=10/0; //模拟抛出异常

        //更新库存量
        return productInfoService.updateStock(id, stock);
    }
}
